﻿using System;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SKN
{
    /// <summary>
    /// Contains common method for WebApplication
    /// </summary>
    public static class WebApplication
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="contentDbSvr"></param>
        /// <param name="contentDbName"></param>
        /// <param name="contentDbLogin"></param>
        /// <param name="contentDbPwd"></param>
        /// <param name="appPoolId"></param>
        /// <param name="appPoolUserName"></param>
        /// <param name="appPoolPwd"></param>
        /// <param name="strSvrComment"></param>
        /// <param name="port"></param>
        public static void CreateWebApplication(string siteName, string contentDbSvr, string contentDbName,
                                                string contentDbLogin, string contentDbPwd,
                                                string appPoolId, string appPoolUserName, string appPoolPwd,
                                                string strSvrComment, int port)
        {
            SPFarm farm = SPFarm.Local;

            // specify Web Application properties
            var webAppBldr = new SPWebApplicationBuilder(farm)
                                 {
                                     ServerComment = strSvrComment,
                                     Port = port,
                                     DefaultZoneUri = new Uri(siteName),
                                     UseNTLMExclusively = true,
                                     ApplicationPoolId = appPoolId,
                                     ApplicationPoolUsername = appPoolUserName
                                 };

            // Specify AppPool to use
            var secString = new SecureString();
            foreach (char c in appPoolPwd)
            {
                secString.AppendChar(c);
            }
            webAppBldr.ApplicationPoolPassword = secString;

            // specify Database name
            if (contentDbName.Length > 0)
            {
                webAppBldr.CreateNewDatabase = true;
                webAppBldr.DatabaseServer = contentDbSvr;
                webAppBldr.DatabaseName = contentDbName;
                // todo: determine which identity can be used 
                //webAppBldr.DatabasePassword = contentDbPwd;
                //webAppBldr.DatabaseUsername = contentDbLogin;
            }

            // elevate permissions to allow user to create a Web App.
            SPSecurity.RunWithElevatedPrivileges(delegate
                                                     {
                                                         // create WSS Web App
                                                         SPWebApplication webApp = webAppBldr.Create();

                                                         // create IIS site
                                                         webApp.Provision();
                                                     });
        }
    }
}